//
//  RyueTabBar.swift
//  RyueWeibo
//
//  Created by jugg on 2016/11/30.
//  Copyright © 2016年 Renyue'husband. All rights reserved.
//

import UIKit

protocol RyueTabBarDelegate: NSObjectProtocol {
    func didClickComponsButton()
}

class RyueTabBar: UITabBar {
    
    
    var callBack: ( ()->())?
    
    weak var tabBarDelegate:RyueTabBarDelegate?

    private lazy var compseButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        
        
        return button
    }()
    
    
  override init(frame: CGRect) {
    super.init(frame: frame)
    setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  @objc private func buttonClick(){
    
    callBack?()
    
        tabBarDelegate?.didClickComponsButton()
        
    }
    
    private func setupUI(){
        self.backgroundImage=UIImage(named: "tabbar_background")
        addSubview(compseButton)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        compseButton.center.x=width/2
        compseButton.center.y=height/2
        
        let buttonWidth = width/5
        
        
        var index = 0
        for i in subviews{
            
            if i.isKind(of: NSClassFromString("UITabBarButton")!) {
                i.width=buttonWidth
                i.x=buttonWidth*CGFloat(index)
                index+=1
                if index==2 {
                    index+=1
                }
            }
           
            
        }
        
        
    }

}
